﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryMangementSystem.Util
{
    static class BookUtil
    {
        //日期格式化
        public static DateTime Format_date(DateTime date)
        {

            String date_time = date.ToShortDateString().ToString();
            DateTime dateTime = DateTime.Parse(date_time);
            return dateTime;
        }
        //文本框只能输入数字
        private static void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            }
        }
        //减库存
        public static int reduceStock(string bookID, LibraryEntities1 library) {
            BookInfo b=library.BookInfo.Single(B => B.BookID == bookID);
            int stock= (int)b.Number;
            if (stock <= 0) {
                return 0;
            }
            stock -= 1;
            b.Number = stock;
            
            return 1;
        }

        public static int addStock(string  bookID, LibraryEntities1 library)
        {
            BookInfo b = library.BookInfo.Single(B => B.BookID == bookID);
            int stock = (int)b.Number;
            stock += 1;
            b.Number = stock;
            
            return 1;
        }
        //减图书证的借书次数
        public static int reduceCert(string certId, LibraryEntities1 library) {
            CertificateInfo certificateInfo = library.CertificateInfo.Single(C=> C.Cert_id==certId);
            int borrowNums= (int)certificateInfo.BorrowNums;
            if (borrowNums<=0) {
                return 0;
            }
            borrowNums -= 1;
            certificateInfo.BorrowNums = borrowNums;
            return 1;

        }
        //加图书证的借书次数
        public static int addCert(string certId, LibraryEntities1 library)
        {
            
            CertificateInfo certificateInfo = library.CertificateInfo.Single(C => C.Cert_id == certId);
            int borrowNums = (int)certificateInfo.BorrowNums;
            if (certificateInfo.Name == "学生证")
            {
                if (borrowNums >= 5)
                {
                    certificateInfo.BorrowNums = 5;
                }

            }
            else if(certificateInfo.Name == "教师证")
            {
                if (borrowNums >= 10)
                {
                    certificateInfo.BorrowNums = 10;
                }
            }
            
            borrowNums += 1;
            certificateInfo.BorrowNums = borrowNums;
            return 1;

        }



    }
}
